package com.gs.wzweather.controller;

import com.gs.wzweather.service.ServiceFileService;
import com.gs.wzweather.service.SevereWeatherService;
import com.gs.wzweather.utils.Constant;
import com.gs.wzweather.utils.HttpUtils;
import com.gs.wzweather.vo.WordPageInfoVO;
import com.gs.wzweather.vo.StormMessageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/18 0018
 */
@Controller
@RequestMapping("file")
public class FileController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ServiceFileService serviceFileService;
    @Autowired
    SevereWeatherService severeWeatherService;

    @RequestMapping("/forcast/important")
    @ResponseBody
    public WordPageInfoVO getImportantWord(Integer pageNum) {
        return serviceFileService.getImportantPageInfo(pageNum, Constant.WordPath.IMPORTANT_FORCAST_PATH);
    }


    @RequestMapping("/forcast/threeMonthly")
    @ResponseBody
    public WordPageInfoVO getTHREEMONTHLYWordName(Integer pageNum) {
        return serviceFileService.getThreeMonthlyPageInfo(pageNum, Constant.WordPath.THREEMONTHLY_FORCAST_PATH);
    }

    @RequestMapping("/forcast/portAndChannel")
    @ResponseBody
    public WordPageInfoVO getPortAndChannelTxtName(Integer pageNum) {
        return serviceFileService.getPortAndChannelPageInfo(pageNum, Constant.WordPath.PORTANDCHANNEL);
    }

    @RequestMapping(value = "/forcast/portAndChannelDown" )
    @ResponseBody
    public void getPortAndChannel(String fileName, HttpServletResponse response) {
        String filePath = Constant.WordPath.PORTANDCHANNEL+fileName;
        File file = new File(filePath);
        System.out.println(fileName);
        if (file.exists())
        HttpUtils.FileDown(file,response,fileName);
    }

    @RequestMapping("/precipitation1/imgNames")
    @ResponseBody
    public List getPrecipitation1ImgNames() {
        return severeWeatherService.getRain1ImgNames();
    }

    @RequestMapping("/precipitation1/img")
    public void getPrecipitation1Img(String imgName, HttpServletResponse response) {
        if (imgName != null)
            severeWeatherService.getRain1Img(imgName, response);
    }

    @RequestMapping("/precipitation3/imgNames")
    @ResponseBody
    public List getPrecipitationImgNames() {
        return severeWeatherService.getRain3ImgNames();
    }

    @RequestMapping("/precipitation3/img")
    public void getPrecipitationImg(String imgName, HttpServletResponse response) {
        if (imgName != null)
            severeWeatherService.getRain3Img(imgName, response);
    }

    @RequestMapping("/storm")
    @ResponseBody
    public StormMessageVO getStorm() {
        return severeWeatherService.getStorm();
    }


}
